#include "../Hdr/Terrain.h"
#include "../Hdr/Chunk.h"
#include <SDL/SDL_opengl.h>
#include <iostream>

Eternal::Scene::Terrain::Terrain()
{
	
}

Eternal::Scene::Terrain::~Terrain()
{
	
}

void Eternal::Scene::Terrain::Load()
{
	Chunks = new Chunk[4];
	
	Chunks[0].gridLocX = -1;	Chunks[0].gridLocZ = -1;
	Chunks[1].gridLocX = 1;		Chunks[1].gridLocZ = -1;
	Chunks[2].gridLocX = 1;		Chunks[2].gridLocZ = 1;
	Chunks[3].gridLocX = -1;	Chunks[3].gridLocZ = 1;
	
	for ( unsigned short x = 1;x < 512;x++ )
	{
		for ( unsigned short z = 1;z < 512;z++ )
		{
			for ( unsigned short y = 1;y < 16;y++ )
			{
				// Initialize everything on the first layer to 1
				// Everything else to 0
				(y == 1) ? i_arrBlocks[x][y][z] = 1 : i_arrBlocks[x][y][z] = 0;
			}
		}
	}
}

void Eternal::Scene::Terrain::Render() const
{
	// Chunk to be updated;
	static unsigned short i_toChunk = 0;
	
	Chunks[0].gridLocX = -1;	Chunks[0].gridLocZ = -1;
	Chunks[1].gridLocX = 1;		Chunks[1].gridLocZ = -1;
	Chunks[2].gridLocX = 1;		Chunks[2].gridLocZ = 1;
	Chunks[3].gridLocX = -1;	Chunks[3].gridLocZ = 1;
	
	glEnableClientState(GL_VERTEX_ARRAY);
	glColor3f(1,1,1);

	/* Render all of the chunk */
	for ( unsigned short i = 0;i < 4;i++ )
	{
		std::cout << i;
		Chunks[i].Render();
	}
	
	i_toChunk = (i_toChunk > 16) ? 0 : i_toChunk+1;
	
	glDisableClientState(GL_VERTEX_ARRAY);
}